App({
  globalData: {
    userInfo: {},
    weixinId: '',
    cookie:'',
    alreadyLoggedIn: false,
  },

  onLaunch() {
    this.loadGlobalData();
  },

  loadGlobalData() {
    const that = this;
    try {
      // 尝试获取持久化的数据
      wx.getStorage({
        key: 'globalData',
        success(res) {
          // 更新全局数据
          that.globalData = res.data;
        },
        fail() {
          console.log('首次加载或没有持久化数据');
        },
      });
    } catch (e) {
      console.error('加载持久化数据失败:', e);
    }
  },

  saveGlobalData() {
    try {
      // 保存全局数据到本地存储
      wx.setStorage({
        key: 'globalData',
        data: this.globalData,
        success() {
          console.log('全局数据已成功持久化');
        },
        fail(err) {
          console.error('持久化全局数据失败:', err);
        },
      });
    } catch (e) {
      console.error('保存持久化数据失败:', e);
    }
  },
  resetglobalData() {
    this.globalData.userInfo = {};
    this.globalData.weixinId = '';
    this.globalData.cookie = '';
    this.globalData.alreadyLoggedIn = false;
    this.saveGlobalData();
  },
});
